#!/usr/bin/env python
#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.

import rollup_hops

import cPickle
import sys

if __name__ == '__main__':
  if len(sys.argv) < 3:
    print 'Usage: %s file_to_roll file2_to_roll ... target_file'
    sys.exit(1)
  data = [cPickle.load(open(filename)) for filename in sys.argv[1:-1]]
  print 'Finished loading, rolling up'
  results = rollup_hops.rollup(data)
  print 'Global stats:'
  print 'Total found VDO fragments: %d' % len(results[0])
  print 'Total values stored: %d' % results[1]
#  print 'Globally unique keys: %d' % len(results[2])
  print 'Bytes in values (not uniqified, sadly): %d' % results[2]
  cPickle.dump({'found_keys': results[0], 'total_stores': results[1],
                #  'unique_keys': results[2],
                'value_bytes': results[2],
                'total_bytes': results[3],
                'bencoded_stores': results[4],
                'bencoded_bytes': results[5],
                'peer_record_stores': results[6],
                'peer_record_bytes': results[7],
                'unvanish_acceptable_stores': results[8],
                'unvanish_acceptable_bytes': results[9],
                'sybil_finds': results[10]},
               open(sys.argv[-1], 'wb'), protocol=cPickle.HIGHEST_PROTOCOL)
